//
//  URLBuilder.swift
//  ScaffoldKit
//
//  Created by SuXinDe on 2018/7/29.
//  Copyright © 2018年 su xinde. All rights reserved.
//

import UIKit

internal enum URLBuilder {
    
    internal static func build(query: [String: String]?) -> String {
        guard let query = query else {
            return ""
        }
        
        var finalQuery: String = ""
        for (index, parameter) in query.enumerated() {
            guard let parameterKey = parameter.key.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed), let parameterValue = parameter.value.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) else {
                continue
            }
            finalQuery += index == 0 ? "?" : "&"
            finalQuery += parameterKey + "=" + parameterValue
        }
        return finalQuery
    }

}
